/*
 * Copyright 2006-2009 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.osgi.config.internal.util;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;

/**
 * Convention callback that transforms "&lt;property-name&gt;-ref" attributes
 * into a bean definition that sets the give &lt;property-name&gt; to a bean
 * reference pointing to the attribute value.
 * 
 * <p/> Thus attribute "comparator-ref='bla'" will have property 'comparator'
 * pointing to bean named 'bla'.
 * 
 * @see BeanDefinitionBuilder#addPropertyReference(String, String)
 * 
 * @author Costin Leau
 */
public class PropertyRefAttributeCallback implements AttributeCallback {

	private static final String PROPERTY_REF = "-ref";


	public boolean process(Element parent, Attr attribute, BeanDefinitionBuilder builder) {
		String name = attribute.getLocalName();
		if (name.endsWith(PROPERTY_REF)) {
			String propertyName = name.substring(0, name.length() - PROPERTY_REF.length());
			builder.addPropertyReference(propertyName, attribute.getValue());
			return false;
		}
		return true;
	}
}
